unit Class_SORT_MATCH;

interface
uses
  Classes,SysUtils,DB,Uni,UniEngine;

type
  TSortMatch=class(TUniEngine)
  private
    FCODE2007 : string;
    FNAME2007 : string;
    FCODE2010 : string;
    FNAME2010 : string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;                         
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property CODE2007 : string read FCODE2007  write FCODE2007;
    property NAME2007 : string read FNAME2007  write FNAME2007;
    property CODE2010 : string read FCODE2010  write FCODE2010;
    property NAME2010 : string read FNAME2010  write FNAME2010;    
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(ASORTMATCH:TSortMatch):TSortMatch;overload;        
    class procedure CopyIt(ASORTMATCH:TSortMatch;var Result:TSortMatch);overload;       
  end;

implementation

{ TSortMatch }
procedure TSortMatch.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('CODE2007').Value := CODE2007;
        ParamByName('NAME2007').Value := NAME2007;
        ParamByName('CODE2010').Value := CODE2010;
        ParamByName('NAME2010').Value := NAME2010; 
      end;
      otEdit:
      begin
        ParamByName('CODE2007').Value := CODE2007;
        ParamByName('NAME2007').Value := NAME2007;
        ParamByName('CODE2010').Value := CODE2010;
        ParamByName('NAME2010').Value := NAME2010; 
      end;  
      otDelt:
      begin
        ParamByName('CODE2007').Value := CODE2007;
        ParamByName('NAME2007').Value := NAME2007;
        ParamByName('CODE2010').Value := CODE2010;
        ParamByName('NAME2010').Value := NAME2010; 
      end;  
    end;
  end;
end;

function TSortMatch.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_SORT_MATCH',['CODE2007',CODE2007,'NAME2007',NAME2007,'CODE2010',CODE2010,'NAME2010',NAME2010],AUniConnection);
end;

function TSortMatch.GetNextIdex: Integer;
begin

end;

function TSortMatch.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TSortMatch.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_SORT_MATCH WHERE   CODE2007=:CODE2007 AND NAME2007=:NAME2007 AND CODE2010=:CODE2010 AND NAME2010=:NAME2010';
end;

function TSortMatch.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_SORT_MATCH'
         +'    ( CODE2007, NAME2007, CODE2010, NAME2010)'
         +'    VALUES'
         +'    (:CODE2007,:NAME2007,:CODE2010,:NAME2010)';
end;

function TSortMatch.GetStrsIndex: string;
begin
  Result:=Format('%S-%S-%S-%S',[CODE2007,NAME2007,CODE2010,NAME2010]);
end;

function TSortMatch.GetStrUpdate: string;
begin
  raise Exception.Create('CAN NOT SUPPORT GETSTRUPDATE METHOD.CAUSE ALL THIS TABLE FILEDS ARE PRIMARY KEY.')
end;

constructor TSortMatch.Create;
begin

end;

destructor TSortMatch.Destroy;
begin

  inherited;
end;

class function TSortMatch.ReadDS(AUniQuery: TUniQuery): TUniEngine;
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  Result:=TSortMatch.Create;

  with TSortMatch(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=Field.FieldName;
      if FieldName='CODE2007' then
      begin
        CODE2007 :=Field.AsString;
      end else
      if FieldName='NAME2007' then
      begin
        NAME2007 :=Field.AsString;
      end else
      if FieldName='CODE2010' then
      begin
        CODE2010 :=Field.AsString;
      end else
      if FieldName='NAME2010' then
      begin
        NAME2010 :=Field.AsString;
      end;
    end
  end;  
end;

class procedure TSortMatch.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  if Result=nil then Exit;

  with TSortMatch(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=Field.FieldName;
      if FieldName='CODE2007' then
      begin
        CODE2007 :=Field.AsString;
      end else
      if FieldName='NAME2007' then
      begin
        NAME2007 :=Field.AsString;
      end else
      if FieldName='CODE2010' then
      begin
        CODE2010 :=Field.AsString;
      end else
      if FieldName='NAME2010' then
      begin
        NAME2010 :=Field.AsString;
      end;
    end
  end;  
end;

class function  TSortMatch.CopyIt(ASORTMATCH: TSortMatch): TSortMatch;
begin
  Result:=TSortMatch.Create;
  TSortMatch.CopyIt(ASORTMATCH,Result)
end;

class procedure TSortMatch.CopyIt(ASORTMATCH:TSortMatch;var Result:TSortMatch);
begin
  if Result=nil then Exit;
  Result.CODE2007 :=ASORTMATCH.CODE2007;
  Result.NAME2007 :=ASORTMATCH.NAME2007;
  Result.CODE2010 :=ASORTMATCH.CODE2010;
  Result.NAME2010 :=ASORTMATCH.NAME2010;
end;

end.

